package com.gali.rabbit.three;

import com.gali.rabbit.utils.RabbitMqUtils;
import com.gali.rabbit.utils.SleepUtils;
import com.rabbitmq.client.CancelCallback;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DeliverCallback;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * @Auther: gali
 * @Date: 2022-08-01 17:58
 * @Description:
 */
public class Work03 {
    // 队列名称
    public static final String TASK_QUEUE_NAME = "ack_queue";

    public static void main(String[] args) throws IOException, TimeoutException {
        Channel channel = RabbitMqUtils.getChannel();
        System.out.println("C1等待时间较短");
        DeliverCallback deliverCallback=(consumerTag,message)->{
            SleepUtils.sleep(1);
            System.out.println("接受到的消息是:"+new String(message.getBody(),"UTF-8"));
            //手动应答 1.消息的标记 2.是否批量应答
            channel.basicAck(message.getEnvelope().getDeliveryTag(), false);
        };
        // 取消消费参数
        CancelCallback cancelCallback = consumerTag -> {
            System.out.println(consumerTag+"消费者取消消费借口回调逻辑");
        };
        //设置不公平分发
        //预取值是2
        int prefetchCount=2;
        channel.basicQos(prefetchCount);
        //手动应答
        boolean autoAck=false;
        channel.basicConsume(TASK_QUEUE_NAME,autoAck,deliverCallback,cancelCallback);
    }
}
